<!--
* description: 文本输入框通用组件（带单位）值列表
* author: zxuan
* createdDate: 2019-11-21
-->
<template>
  <el-col :span="span" v-show="showow">
    <lableName
      :validrule="validrule"
      :curLabelName="curLabelName"
      :isShowLabel="isShowLabel"
      :isRequire="isRequire"
    ></lableName>
    <el-input
      :style="setInputWidth(ShowUnit)"
      :placeholder="readonly ? '' : '请输入'"
      v-model="modelCode"
      :disabled="disabled"
      :readonly="readonly"
      size="small"
      :suffix-icon="readonly ? '' : 'el-icon-search'"
      @clear="clearCode"
      clearable
      resize="none"
      @blur="sendCode1"
      :type="inputType"
    ></el-input>
    <el-select v-model="unit" v-show="ShowUnit.select" style="width:20%" @change="sendCode2">
      <el-option
        v-for="item in options"
        :key="item.lookupValueCode"
        :label="item.lookupValueName"
        :value="item.lookupValueCode"
      ></el-option>
    </el-select>
    <span style="height:29px;line-height:29px" v-show="!ShowUnit.select">{{ShowUnit.text}}</span>
    <validateFormate :curLabelName="curLabelName" :validrule="validrule"></validateFormate>
  </el-col>
</template>
<script>
import { inputMixins } from "@/components/mixins/inputMixins";
import { crmApis } from "@/api/apiList/crm";
import { requestAPI } from "@/api/commonRequest";
export default {
  name: "crmTextInput",
  mixins: [inputMixins],
  components: {
    lableName: () => import("@/components/lableName"), //标题溢出样式组件
    validateFormate: () => import("@/components/validateFormate") //校验提示样式组件
  },
  props: {
    show: {
      type: Boolean,
      default: true
    },
    isShowUnit: {
      text: {
        type: String,
        default: ""
      },
      select: {
        type: Boolean,
        default: true
      }
    },
    isRequire: {
      type: Boolean,
      default: false
    },
    getVal: { type: String, default: "" },
  },
  data() {
    return {
      unit: "1",
      showow: this.show,
      ShowUnit: {
        text: this.isShowUnit.text,
        select: this.isShowUnit.select
      },
      // placeholder: this.readonly ? "" : "请输入",
      //suffixIcon: this.readonly ? "" : "el-icon-search"
      options: this.getOptions(),


    };
  },
  mounted() {
    this.$nextTick(function() {
      this.Init();
    });
  },
  watch: {
    getVal(val) {
      debugger
      this.unit=val
    }
  },
  methods: {
    getData(val) {
      debugger;
      // this.isRequire=this.isRequire
      if (val == "1") {
        this.isRequire = false;
      } else {
        this.isRequire = true;
      }
    },
    sendCode1() {
      let val = this.modelCode;
      let text = this.codeField;
      this.$emit("changeCode", val, text);
      this.$emit("changeblur", val);
      this.sendCode();
      let str = this.codeField;
      if (str) {
        this.$crmcf.__getParent(this, "formField", function(formField) {
          if (val != null) {
            formField[str] = val;
          } else {
            formField[str] = "";
          }
        });
      }
    },
    setInputWidth(ShowUnit) {
      if (ShowUnit.select) {
        return "width:46%";
      } else {
        return "width:64%";
      }
    },
    getOptions: function() {
      let that = this;
      let queryObj = {
        // api配置
        apiConfig: crmApis.mdsLookupValueQueryByPage,
        // 需要调用的API服务配置
        apiServices: [
          {
            //表格中台返回网格的字段
            apiQueryRow: ["lookupValueCode", "lookupValueName"]
          }
        ],
        //条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          pageSize: 10,
          pageIndex: 1,
          //当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo: { lookupTypeCode: "LX197" } //值列表编码
        }
      };
      let paramD = that.$getParams(queryObj);
      // 调用中台API方法（可复用）
      requestAPI(paramD).then(response => {
        if (
          response.result == "1"
        ) {
          that.options = response.rows;
        }
      });
    },
    sendCode2(val) {
      debugger;
      this.$emit("emitCode", val);
      let str = this.textField;
      if (str) {
        this.$crmcf.__getParent(this, "formField", function(formField) {
          if (val != null) {
            formField[str] = val;
          } else {
            formField[str] = "";
          }
        });
      }
    },
    Init() {
      debugger;
      let that = this;
      let str = this.textField;
      if (str) {
        this.$crmcf.__getParent(this, "formField", function(formField) {
          if (str != null) {
            that.unit = formField[str];
          } else {
            that.unit = "1";
          }
        });
      }
      else{
        that.unit = "1";
      }
    }
  }
};
</script>
